package com.weixing.mall.base.wrapper;

import cn.hutool.core.util.StrUtil;
import com.weixing.mall.base.enums.ResultCodeEnum;

/**
 * 统一返回参数工具类
 *
 * @author siashan
 * @since  v1.0
 */
public class ResultUtil {

    /**
     * 默认成功请求返回信息
     *
     * @return   报文
     */

    public static Result success() {
        return result(ResultCodeEnum.SUCCESS);
    }

    /**
     * 自定义返回信息
     *
     * @param resultCodeEnum    返回值枚举
     * @return   报文
     */
    public static Result success(ResultCodeEnum resultCodeEnum) {
        return new Result(ResultCodeEnum.SUCCESS.getCode(), resultCodeEnum.getCode(),resultCodeEnum.getMsg());
    }




    /**
     * 自定义返回信息
     *
     * @param resultCodeEnum    枚举
     * @param msg   返回信息
     * @return   报文
     */
    public static Result success(ResultCodeEnum resultCodeEnum,String... msg) {
        return new Result(ResultCodeEnum.SUCCESS.getCode(), resultCodeEnum.getCode(), StrUtil.format(resultCodeEnum.getMsg(),msg));
    }

    /**
     * 自定义返回信息
     *
     * @param msg   返回信息
     * @return   报文
     */
    public static Result success(String msg) {
        return new Result(ResultCodeEnum.SUCCESS.getCode(), msg);
    }



    public static <T> Result<T> success(T data) {
        return success().setData(data);
    }


    public static <T> Result<T> successData(T data) {
        return success().setData(data);
    }

    public static <T> Result<T> success(ResultCodeEnum resultCodeEnum,T data){
        return success(resultCodeEnum).setData(data);
    }

    public static <T> Result<T> success(String msg,T data){
        return success(msg).setData(data);
    }


    public static Result error(){
        return result(ResultCodeEnum.ERROR);
    }

    public static Result error(ResultCodeEnum resultCodeEnum){
        return new Result(ResultCodeEnum.ERROR.getCode(),resultCodeEnum.getCode(),resultCodeEnum.getMsg());
    }

    public static  Result error(String msg){
        return new Result(ResultCodeEnum.ERROR.getCode(),msg);
    }

    public static  Result error(int code,String msg){
        return new Result(code,msg);
    }


    /**
     * 用户未认证
     *
     * @return  报文
     */

    public static Result unauthorized() {
        return result(ResultCodeEnum.UNAUTHORIZED);
    }


    /**
     * 用户认证失败
     *
     * @return 报文
     */

    public static Result authorizedFail() {
        return result(ResultCodeEnum.AUTHORIZED_FAIL);
    }


    /**
     * 用户认证失败
     * @param msg  返回消息
     * @return   报文
     */
    public static Result authorizedFail(String msg) {
        return result(ResultCodeEnum.SUCCESS,ResultCodeEnum.AUTHORIZED_FAIL,msg);
    }


    public static Result unauthorized(ResultCodeEnum resultCodeEnum) {
        return new Result(ResultCodeEnum.UNAUTHORIZED.getCode(),resultCodeEnum.getCode(),resultCodeEnum.getMsg());
    }

    public static Result unauthorized(String msg) {
        return new Result(ResultCodeEnum.UNAUTHORIZED.getCode(),msg);
    }


    /**
     * 访问拒绝
     *
     * @return   报文
     */

    public static Result accessDenied() {
        return result(ResultCodeEnum.ACCESS_DENIED);
    }


    /**
     * 404 not found
     *
     * @return   报文
     */
    public static Result notFound() {
        return result(ResultCodeEnum.NOT_FOUND);
    }


    public static Result accessDenied(ResultCodeEnum resultCodeEnum) {
        return new Result(ResultCodeEnum.ACCESS_DENIED.getCode(),resultCodeEnum.getCode(),resultCodeEnum.getMsg());
    }

    /**
     * 系统异常
     *
     * @return   报文
     */
    public static Result exception() {
        return result(ResultCodeEnum.EXCEPTION);
    }

    /**
     * 服务熔断
     *
     * @return   报文
     */
    public static Result fallBack() {
        return result(ResultCodeEnum.GATEWAY_TIMEOUT);
    }


    private static Result result(ResultCodeEnum resultCodeEnum){
        return new Result(resultCodeEnum.getCode(),resultCodeEnum.getMsg());
    }


    private static Result result(ResultCodeEnum resultCodeEnum,String msg){
        return new Result(resultCodeEnum.getCode(),msg);
    }

    private static Result result(ResultCodeEnum mainCode,ResultCodeEnum subCode,String msg){
        return new Result(mainCode.getCode(),subCode.getCode(),msg);
    }
}
